package board.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import board.dao.MemberDAO;
import board.vo.MemberVO;

import com.opensymphony.xwork2.ActionSupport;

public class MemberAction extends ActionSupport implements SessionAware{

	private String id;
	private String passwd;
	private boolean login_success;
	
	private MemberVO memberVO;
	private Map<String, Object> session;
	private MemberDAO dao = new MemberDAO();
	
	public String goLogin(){
		
		memberVO = dao.selectMember(id);
		
		if(memberVO == null){
			System.out.println("！！" + id + "存在しないユーザID！！");
			return SUCCESS;
		}
		
		if(!memberVO.getPasswd().equals(passwd)){
			login_success = false;
			System.out.println("！！パスワード不一致！！");
		}
		else{
			login_success = true;
			session.put("login_id", id);
			
			int count = memberVO.getCount() + 1; 
			memberVO.setCount(count);
			dao.update(memberVO);
			
			memberVO = dao.selectMember(id);
			System.out.println(id + "　ログイン成功");
		}
		return SUCCESS;
	}
	
	public String changePasswd(){
		
		memberVO = dao.selectMember(id);
		memberVO.setPasswd(passwd);
		dao.update(memberVO);
		
		System.out.println(id + " パスワード変更");
		
		return SUCCESS;
	}
	
	public String goLogout(){
		
		session.remove("login_id");
		
		return SUCCESS;
	}


	//getter setter
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPasswd() {
		return passwd;
	}
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
	public boolean getLogin_success() {
		return login_success;
	}
	public void setLogin_success(boolean login_success) {
		this.login_success = login_success;
	}
	public MemberVO getMemberVO() {
		return memberVO;
	}
	public void setMemberVO(MemberVO memberVO) {
		this.memberVO = memberVO;
	}
	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}
	
}
